{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "88b58cd8-53b1-4ef1-9232-563430da1c98",
   "metadata": {},
   "outputs": [],
   "source": [
    "api_url=\"https://api.zetatechs.com/v1\"\n",
    "api_key=\"sk-TdHgyILmI69vVZ9S885unvFRqOX5Nil9jCDKg5YC0gucA2EJ\"\n",
    "from langchain_openai import ChatOpenAI\n",
    "model=ChatOpenAI(model=\"gpt-3.5-turbo-16k\",base_url=api_url,api_key=api_key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9a32a6fb-ae1f-49ad-9d74-c07263901d73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='你好王者荣耀，很高兴认识你！有什么可以帮助你的吗？', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 34, 'prompt_tokens': 20, 'total_tokens': 54, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': 'fp_0165350fbb', 'id': 'chatcmpl-Bp4BFE9rr4ZoaxPCoGtICLTLSF8Rn', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--4ae6ceab-ac64-4223-b26f-9851c6265451-0', usage_metadata={'input_tokens': 20, 'output_tokens': 34, 'total_tokens': 54, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import HumanMessage\n",
    "model.invoke([HumanMessage(content=\"你好，我叫王者荣耀\")])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1b1b3435-a1bc-4981-9d72-1218e9c2b2e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='你好，你的名字是什么？', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 12, 'prompt_tokens': 16, 'total_tokens': 28, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': 'fp_0165350fbb', 'id': 'chatcmpl-Bp4BJRoJWkNOIbfnWWpmC0QsCDxGl', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--9a0291a4-c858-4133-98fe-76fedf5b7ab3-0', usage_metadata={'input_tokens': 16, 'output_tokens': 12, 'total_tokens': 28, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.invoke([HumanMessage(content=\"你好，我叫什么\")])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a2350288-043e-40a8-9f67-17e34620e6e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='很抱歉，您没有告诉我您的名字。您可以告诉我您的名字，这样我就能知道您叫什么了。', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 44, 'prompt_tokens': 53, 'total_tokens': 97, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': 'fp_0165350fbb', 'id': 'chatcmpl-Bp4BLwpc5c8lAvYMT3Z3Jb1qKmF0e', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--f8706146-ae0a-460f-a1aa-73c8f3ff82a8-0', usage_metadata={'input_tokens': 53, 'output_tokens': 44, 'total_tokens': 97, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import AIMessage\n",
    "\n",
    "model.invoke(\n",
    "    [\n",
    "        HumanMessage(content=\"你好，我叫王者荣耀\"),\n",
    "        AIMessage(content=\"你好，王者荣耀，需要我做什么?\"),\n",
    "        HumanMessage(content=\"我叫什么\")\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "21f47e68-e123-4005-bb68-b67f840106a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting langchain_community\n",
      "  Downloading langchain_community-0.3.26-py3-none-any.whl.metadata (2.9 kB)\n",
      "Requirement already satisfied: langchain-core<1.0.0,>=0.3.66 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain_community) (0.3.67)\n",
      "Requirement already satisfied: langchain<1.0.0,>=0.3.26 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain_community) (0.3.26)\n",
      "Requirement already satisfied: SQLAlchemy<3,>=1.4 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain_community) (2.0.41)\n",
      "Requirement already satisfied: requests<3,>=2 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain_community) (2.32.4)\n",
      "Requirement already satisfied: PyYAML>=5.3 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain_community) (6.0.2)\n",
      "Collecting aiohttp<4.0.0,>=3.8.3 (from langchain_community)\n",
      "  Downloading aiohttp-3.12.13-cp313-cp313-win_amd64.whl.metadata (7.9 kB)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<10,>=8.1.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain_community) (9.1.2)\n",
      "Collecting dataclasses-json<0.7,>=0.5.7 (from langchain_community)\n",
      "  Downloading dataclasses_json-0.6.7-py3-none-any.whl.metadata (25 kB)\n",
      "Collecting pydantic-settings<3.0.0,>=2.4.0 (from langchain_community)\n",
      "  Downloading pydantic_settings-2.10.1-py3-none-any.whl.metadata (3.4 kB)\n",
      "Requirement already satisfied: langsmith>=0.1.125 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain_community) (0.4.4)\n",
      "Collecting httpx-sse<1.0.0,>=0.4.0 (from langchain_community)\n",
      "  Downloading httpx_sse-0.4.1-py3-none-any.whl.metadata (9.4 kB)\n",
      "Collecting numpy>=2.1.0 (from langchain_community)\n",
      "  Downloading numpy-2.3.1-cp313-cp313-win_amd64.whl.metadata (60 kB)\n",
      "Collecting aiohappyeyeballs>=2.5.0 (from aiohttp<4.0.0,>=3.8.3->langchain_community)\n",
      "  Downloading aiohappyeyeballs-2.6.1-py3-none-any.whl.metadata (5.9 kB)\n",
      "Collecting aiosignal>=1.1.2 (from aiohttp<4.0.0,>=3.8.3->langchain_community)\n",
      "  Downloading aiosignal-1.3.2-py2.py3-none-any.whl.metadata (3.8 kB)\n",
      "Collecting attrs>=17.3.0 (from aiohttp<4.0.0,>=3.8.3->langchain_community)\n",
      "  Downloading attrs-25.3.0-py3-none-any.whl.metadata (10 kB)\n",
      "Collecting frozenlist>=1.1.1 (from aiohttp<4.0.0,>=3.8.3->langchain_community)\n",
      "  Downloading frozenlist-1.7.0-cp313-cp313-win_amd64.whl.metadata (19 kB)\n",
      "Collecting multidict<7.0,>=4.5 (from aiohttp<4.0.0,>=3.8.3->langchain_community)\n",
      "  Downloading multidict-6.6.3-cp313-cp313-win_amd64.whl.metadata (5.4 kB)\n",
      "Collecting propcache>=0.2.0 (from aiohttp<4.0.0,>=3.8.3->langchain_community)\n",
      "  Downloading propcache-0.3.2-cp313-cp313-win_amd64.whl.metadata (12 kB)\n",
      "Collecting yarl<2.0,>=1.17.0 (from aiohttp<4.0.0,>=3.8.3->langchain_community)\n",
      "  Downloading yarl-1.20.1-cp313-cp313-win_amd64.whl.metadata (76 kB)\n",
      "Collecting marshmallow<4.0.0,>=3.18.0 (from dataclasses-json<0.7,>=0.5.7->langchain_community)\n",
      "  Downloading marshmallow-3.26.1-py3-none-any.whl.metadata (7.3 kB)\n",
      "Collecting typing-inspect<1,>=0.4.0 (from dataclasses-json<0.7,>=0.5.7->langchain_community)\n",
      "  Downloading typing_inspect-0.9.0-py3-none-any.whl.metadata (1.5 kB)\n",
      "Requirement already satisfied: langchain-text-splitters<1.0.0,>=0.3.8 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain<1.0.0,>=0.3.26->langchain_community) (0.3.8)\n",
      "Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain<1.0.0,>=0.3.26->langchain_community) (2.10.3)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain_community) (1.33)\n",
      "Requirement already satisfied: packaging<25,>=23.2 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain_community) (24.2)\n",
      "Requirement already satisfied: typing-extensions>=4.7 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain_community) (4.12.2)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.66->langchain_community) (3.0.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from pydantic<3.0.0,>=2.7.4->langchain<1.0.0,>=0.3.26->langchain_community) (0.6.0)\n",
      "Requirement already satisfied: pydantic-core==2.27.1 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from pydantic<3.0.0,>=2.7.4->langchain<1.0.0,>=0.3.26->langchain_community) (2.27.1)\n",
      "Requirement already satisfied: python-dotenv>=0.21.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from pydantic-settings<3.0.0,>=2.4.0->langchain_community) (1.1.0)\n",
      "Collecting typing-inspection>=0.4.0 (from pydantic-settings<3.0.0,>=2.4.0->langchain_community)\n",
      "  Using cached typing_inspection-0.4.1-py3-none-any.whl.metadata (2.6 kB)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from requests<3,>=2->langchain_community) (3.4.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from requests<3,>=2->langchain_community) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from requests<3,>=2->langchain_community) (2.4.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from requests<3,>=2->langchain_community) (2025.4.26)\n",
      "Requirement already satisfied: greenlet>=1 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain_community) (3.2.3)\n",
      "Collecting mypy-extensions>=0.3.0 (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain_community)\n",
      "  Downloading mypy_extensions-1.1.0-py3-none-any.whl.metadata (1.1 kB)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langsmith>=0.1.125->langchain_community) (0.28.1)\n",
      "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langsmith>=0.1.125->langchain_community) (3.10.18)\n",
      "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langsmith>=0.1.125->langchain_community) (1.0.0)\n",
      "Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from langsmith>=0.1.125->langchain_community) (0.23.0)\n",
      "Requirement already satisfied: anyio in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from httpx<1,>=0.23.0->langsmith>=0.1.125->langchain_community) (4.7.0)\n",
      "Requirement already satisfied: httpcore==1.* in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from httpx<1,>=0.23.0->langsmith>=0.1.125->langchain_community) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith>=0.1.125->langchain_community) (0.16.0)\n",
      "Requirement already satisfied: sniffio>=1.1 in d:\\users\\lenovo\\anaconda3\\envs\\llm\\lib\\site-packages (from anyio->httpx<1,>=0.23.0->langsmith>=0.1.125->langchain_community) (1.3.0)\n",
      "Downloading langchain_community-0.3.26-py3-none-any.whl (2.5 MB)\n",
      "   ---------------------------------------- 0.0/2.5 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.5 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.5 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.5 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.5 MB ? eta -:--:--\n",
      "   ---- ----------------------------------- 0.3/2.5 MB ? eta -:--:--\n",
      "   ---- ----------------------------------- 0.3/2.5 MB ? eta -:--:--\n",
      "   -------- ------------------------------- 0.5/2.5 MB 600.7 kB/s eta 0:00:04\n",
      "   -------- ------------------------------- 0.5/2.5 MB 600.7 kB/s eta 0:00:04\n",
      "   ------------ --------------------------- 0.8/2.5 MB 543.6 kB/s eta 0:00:04\n",
      "   ---------------- ----------------------- 1.0/2.5 MB 745.0 kB/s eta 0:00:02\n",
      "   -------------------- ------------------- 1.3/2.5 MB 783.5 kB/s eta 0:00:02\n",
      "   ------------------------ --------------- 1.6/2.5 MB 814.4 kB/s eta 0:00:02\n",
      "   ------------------------ --------------- 1.6/2.5 MB 814.4 kB/s eta 0:00:02\n",
      "   ----------------------------- ---------- 1.8/2.5 MB 820.9 kB/s eta 0:00:01\n",
      "   --------------------------------- ------ 2.1/2.5 MB 870.6 kB/s eta 0:00:01\n",
      "   ---------------------------------------- 2.5/2.5 MB 918.6 kB/s eta 0:00:00\n",
      "Downloading aiohttp-3.12.13-cp313-cp313-win_amd64.whl (446 kB)\n",
      "Downloading dataclasses_json-0.6.7-py3-none-any.whl (28 kB)\n",
      "Downloading httpx_sse-0.4.1-py3-none-any.whl (8.1 kB)\n",
      "Downloading marshmallow-3.26.1-py3-none-any.whl (50 kB)\n",
      "Downloading multidict-6.6.3-cp313-cp313-win_amd64.whl (45 kB)\n",
      "Downloading pydantic_settings-2.10.1-py3-none-any.whl (45 kB)\n",
      "Downloading typing_inspect-0.9.0-py3-none-any.whl (8.8 kB)\n",
      "Downloading yarl-1.20.1-cp313-cp313-win_amd64.whl (86 kB)\n",
      "Downloading aiohappyeyeballs-2.6.1-py3-none-any.whl (15 kB)\n",
      "Downloading aiosignal-1.3.2-py2.py3-none-any.whl (7.6 kB)\n",
      "Downloading attrs-25.3.0-py3-none-any.whl (63 kB)\n",
      "Downloading frozenlist-1.7.0-cp313-cp313-win_amd64.whl (43 kB)\n",
      "Downloading mypy_extensions-1.1.0-py3-none-any.whl (5.0 kB)\n",
      "Downloading numpy-2.3.1-cp313-cp313-win_amd64.whl (12.7 MB)\n",
      "   ---------------------------------------- 0.0/12.7 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/12.7 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/12.7 MB ? eta -:--:--\n",
      "    --------------------------------------- 0.3/12.7 MB ? eta -:--:--\n",
      "   - -------------------------------------- 0.5/12.7 MB 685.5 kB/s eta 0:00:18\n",
      "   - -------------------------------------- 0.5/12.7 MB 685.5 kB/s eta 0:00:18\n",
      "   -- ------------------------------------- 0.8/12.7 MB 748.3 kB/s eta 0:00:16\n",
      "   --- ------------------------------------ 1.0/12.7 MB 794.3 kB/s eta 0:00:15\n",
      "   ---- ----------------------------------- 1.3/12.7 MB 834.7 kB/s eta 0:00:14\n",
      "   ---- ----------------------------------- 1.6/12.7 MB 904.1 kB/s eta 0:00:13\n",
      "   ----- ---------------------------------- 1.8/12.7 MB 944.6 kB/s eta 0:00:12\n",
      "   ------ --------------------------------- 2.1/12.7 MB 981.9 kB/s eta 0:00:11\n",
      "   ------- -------------------------------- 2.4/12.7 MB 1.0 MB/s eta 0:00:11\n",
      "   -------- ------------------------------- 2.6/12.7 MB 1.1 MB/s eta 0:00:10\n",
      "   --------- ------------------------------ 3.1/12.7 MB 1.2 MB/s eta 0:00:09\n",
      "   ---------- ----------------------------- 3.4/12.7 MB 1.2 MB/s eta 0:00:08\n",
      "   ------------ --------------------------- 3.9/12.7 MB 1.3 MB/s eta 0:00:07\n",
      "   ------------- -------------------------- 4.2/12.7 MB 1.3 MB/s eta 0:00:07\n",
      "   -------------- ------------------------- 4.7/12.7 MB 1.3 MB/s eta 0:00:07\n",
      "   ---------------- ----------------------- 5.2/12.7 MB 1.4 MB/s eta 0:00:06\n",
      "   ------------------ --------------------- 6.0/12.7 MB 1.5 MB/s eta 0:00:05\n",
      "   -------------------- ------------------- 6.6/12.7 MB 1.6 MB/s eta 0:00:04\n",
      "   ----------------------- ---------------- 7.3/12.7 MB 1.7 MB/s eta 0:00:04\n",
      "   ----------------------- ---------------- 7.3/12.7 MB 1.7 MB/s eta 0:00:04\n",
      "   ------------------------ --------------- 7.9/12.7 MB 1.6 MB/s eta 0:00:03\n",
      "   --------------------------- ------------ 8.7/12.7 MB 1.7 MB/s eta 0:00:03\n",
      "   ----------------------------- ---------- 9.4/12.7 MB 1.8 MB/s eta 0:00:02\n",
      "   -------------------------------- ------- 10.2/12.7 MB 1.9 MB/s eta 0:00:02\n",
      "   ---------------------------------- ----- 11.0/12.7 MB 2.0 MB/s eta 0:00:01\n",
      "   ------------------------------------- -- 12.1/12.7 MB 2.1 MB/s eta 0:00:01\n",
      "   ---------------------------------------- 12.7/12.7 MB 2.1 MB/s eta 0:00:00\n",
      "Downloading propcache-0.3.2-cp313-cp313-win_amd64.whl (40 kB)\n",
      "Using cached typing_inspection-0.4.1-py3-none-any.whl (14 kB)\n",
      "Installing collected packages: typing-inspection, propcache, numpy, mypy-extensions, multidict, marshmallow, httpx-sse, frozenlist, attrs, aiohappyeyeballs, yarl, typing-inspect, aiosignal, pydantic-settings, dataclasses-json, aiohttp, langchain_community\n",
      "\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   ---- -----------------------------------  2/17 [numpy]\n",
      "   --------- ------------------------------  4/17 [multidict]\n",
      "   --------------------- ------------------  9/17 [aiohappyeyeballs]\n",
      "   -------------------------------- ------- 14/17 [dataclasses-json]\n",
      "   ----------------------------------- ---- 15/17 [aiohttp]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ------------------------------------- -- 16/17 [langchain_community]\n",
      "   ---------------------------------------- 17/17 [langchain_community]\n",
      "\n",
      "Successfully installed aiohappyeyeballs-2.6.1 aiohttp-3.12.13 aiosignal-1.3.2 attrs-25.3.0 dataclasses-json-0.6.7 frozenlist-1.7.0 httpx-sse-0.4.1 langchain_community-0.3.26 marshmallow-3.26.1 multidict-6.6.3 mypy-extensions-1.1.0 numpy-2.3.1 propcache-0.3.2 pydantic-settings-2.10.1 typing-inspect-0.9.0 typing-inspection-0.4.1 yarl-1.20.1\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  WARNING: The scripts f2py.exe and numpy-config.exe are installed in 'D:\\Users\\Lenovo\\anaconda3\\envs\\LLM\\Scripts' which is not on PATH.\n",
      "  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\n"
     ]
    }
   ],
   "source": [
    "pip install langchain_community"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b111ef33-62c0-4ce9-98c4-5b036835ec38",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.chat_history import InMemoryChatMessageHistory\n",
    "from langchain_core.runnables.history import RunnableWithMessageHistory\n",
    "\n",
    "store = {}\n",
    "\n",
    "\n",
    "def get_session_history(session_id: str):\n",
    "    if session_id not in store:\n",
    "        store[session_id] = InMemoryChatMessageHistory()\n",
    "    return store[session_id]\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "\n",
    "parser = StrOutputParser()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b08cb9cd-0a3e-4b0a-9216-bfb54ce84e77",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain=model|parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "712736e1-6bf5-48e5-b1a0-84fbdf299d3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#实例化\n",
    "with_message_history = RunnableWithMessageHistory(chain, get_session_history)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "35ff2fef-df52-4637-aa6e-ced3ae9b9a0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "config1 = {\"configurable\": {\"session_id\": \"1\"}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "fabe04a0-84be-4ab8-9497-39404daa2710",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello Bob! It seems like we already met. How can I assist you today?'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with_message_history.invoke(\n",
    "    [HumanMessage(content=\"Hi! I'm Bob\")],\n",
    "    config=config1,\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "07dcb647-abb8-42f1-98af-49bd116cedbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Your name is Bob.'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = with_message_history.invoke(\n",
    "    [HumanMessage(content=\"What's my name?\")],\n",
    "    config=config1,\n",
    ")\n",
    "parser.invoke(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a748c9dc-e807-4779-8be7-3ae6812a99af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I apologize, but I do not have the capability to know your name as I am a text-based AI assistant and do not have access to personal information about users.'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "config2 = {\"configurable\": {\"session_id\": \"2\"}}\n",
    "\n",
    "with_message_history.invoke(\n",
    "    [HumanMessage(content=\"What's my name?\")],\n",
    "    config=config2,\n",
    ")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "fade9dd8-3a15-40a5-b649-39c4297ee54a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Your name is Bob.'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with_message_history.invoke(\n",
    "    [HumanMessage(content=\"What's my name?\")],\n",
    "    config=config1,\n",
    ")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5e28bec5-90d4-4d5c-b009-1706ddccb983",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'很抱歉听到您没有吃午餐。您现在想让我为您做些什么？'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = with_message_history.invoke(\n",
    "    [HumanMessage(content=\"我今天中午没有吃饭\")],\n",
    "    config=config1,\n",
    ")\n",
    "\n",
    "response.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4a7113eb-6e2d-417d-98ab-fe804c0d5c4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'根据您之前的说法，您今天中午没有吃饭。如果您有其他问题或需要帮助，请告诉我！'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = with_message_history.invoke(\n",
    "    [HumanMessage(content=\"我今天中午吃饭了吗\")],\n",
    "    config=config1,\n",
    ")\n",
    "\n",
    "response.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "35141c93-3a35-4a59-af65-bcb91c1b1d6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\n",
    "            \"system\",\n",
    "            \"You are a helpful assistant. Answer all questions to the best of your ability.\",\n",
    "        ),\n",
    "        MessagesPlaceholder(variable_name=\"messages\"),\n",
    "    ]\n",
    ")\n",
    "\n",
    "chain = prompt | model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "5695d6be-94df-4baa-aa9e-078cfe6608fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello, Bob! How can I assist you today?'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = chain.invoke({\"messages\": [HumanMessage(content=\"hi! I'm bob\")]})\n",
    "\n",
    "response.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "73448502-1107-4c57-b5cf-e65afe5e1999",
   "metadata": {},
   "outputs": [],
   "source": [
    "with_message_history = RunnableWithMessageHistory(chain, get_session_history)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c150f0b0-9d54-4b9a-b16d-f311ebb353f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "config3 = {\"configurable\": {\"session_id\": \"3\"}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d5ff20d5-0c42-4bd6-9d4e-75061d4ea4d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Hello, Jim! How can I assist you today?'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = with_message_history.invoke(\n",
    "    [HumanMessage(content=\"Hi! I'm Jim\")],\n",
    "    config=config3,\n",
    ")\n",
    "\n",
    "response.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "18d60a66-9721-4382-8373-e90a6264aa69",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\n",
    "            \"system\",\n",
    "            \"You are a helpful assistant. Answer all questions to the best of your ability in {language}.\",\n",
    "        ),\n",
    "        MessagesPlaceholder(variable_name=\"messages\"),\n",
    "    ]\n",
    ")\n",
    "\n",
    "chain = prompt | model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "968d86af-b4d7-4e0c-999a-c62b578565ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好，杰克！有什么可以帮助你的吗？'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = chain.invoke(\n",
    "    {\"messages\": [HumanMessage(content=\"我是杰克\")], \"language\": \"\"}\n",
    ")\n",
    "\n",
    "\n",
    "response.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "bd1dfab6-a1e0-429b-a8c4-36bea3bb806a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'很抱歉，作为一个语言模型AI助手，我无法知道您的名字。请随意告诉我您的名字，我将尽力回答您的问题。'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = chain.invoke(\n",
    "    {\"messages\": [HumanMessage(content=\"我的名字是什么\")], \"language\": \"\"}\n",
    ")\n",
    "\n",
    "response.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "1bba30cc-b02f-4ddf-b2a1-d43499a72957",
   "metadata": {},
   "outputs": [],
   "source": [
    "with_message_history = RunnableWithMessageHistory(\n",
    "    chain,\n",
    "    get_session_history,\n",
    "    input_messages_key=\"messages\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "e0a4c100-d132-44bf-b678-2bb25a1ce57e",
   "metadata": {},
   "outputs": [],
   "source": [
    "config4 = {\"configurable\": {\"session_id\": \"4\"}}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b34904ff-3716-41fc-bab7-c8b4f7835b26",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "22c15835-1820-4602-8e8d-fcd2a6dc40fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'¡Hola Todd! ¿En qué puedo ayudarte hoy?'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response = with_message_history.invoke(\n",
    "    {\"messages\": [HumanMessage(content=\"hi! I'm todd\")], \"language\": \"Spanish\"},\n",
    "    config=config,\n",
    ")\n",
    "\n",
    "response.content"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "LLM",
   "language": "python",
   "name": "llm"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
